﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using PluginDB;

namespace App_Code.Helpers
{
    /// <summary>
    /// Summary description for PluginDBHandler
    /// </summary>
    public sealed class PluginDBHandler
    {
        private readonly DBManager _dbManager;
        //private readonly object _lock_object = new object();

        #region Ctor
        PluginDBHandler()
        {
            _dbManager = new DBManager();
        }

        public static PluginDBHandler Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested()
            {
            }

            internal static readonly PluginDBHandler instance = new PluginDBHandler();
        }
        #endregion

        internal List<Category> GetCategoriesList()
        {
            return _dbManager.GetCategoriesList();
        }

        internal List<Video> GetLatestMoviesList()
        {
            return _dbManager.GetLatestMoviesList();
        }

        internal List<TVShow> GetLatestTVShowsList()
        {
            return _dbManager.GetLatestTVShowsList();
        }

        internal List<Video> GetHebSubMovies()
        {
            return _dbManager.GetHebSubMovies();
        }

        internal List<Series> GetHebSubTVShows()
        {
            return _dbManager.GetHebSubTVShows();
        }

        internal bool IsVideoExists(string pluginUrl)
        {
            var exists = _dbManager.IsTVShowExists(pluginUrl);
            if(!exists)
                exists = _dbManager.IsMovieExists(pluginUrl);

            return exists;
        }

        internal bool IsSeriesExists(string seriesName)
        {
            return _dbManager.IsSeriesExists(seriesName);
        }

        internal void AddNewVideo(PluginVideoInfo pluginVideoInfo)
        {
            if (IsVideoExists(pluginVideoInfo.PluginUrl)) return;

            Debug.WriteLine(string.Format("At {0} add movie {1}", DateTime.Now, pluginVideoInfo.VideoName));

            _dbManager.AddVideo(pluginVideoInfo);
        }

        internal List<Site> GetSitesNames()
        {
            return _dbManager.GetSitesNames();
        }

        internal List<Video> GetMoviesByCategoryId(long categoryId)
        {
            return _dbManager.GetMoviesByCategoryId(categoryId);
        }

        internal List<TVShow> GetTVShowsByCategoryId(long categoryId)
        {
            return _dbManager.GetTVShowsByCategoryId(categoryId);
        }

        internal List<Video> GetHebDubMovies()
        {
            return _dbManager.GetHebDubMovies();
        }

        internal List<Video> GetAtoZMoviesList(string startWord)
        {
            return _dbManager.GetAtoZMoviesList(startWord);
        }

        internal List<Series> GetAtoZSeriesList(string startWord)
        {
            return _dbManager.GetAtoZSeriesList(startWord);
        }

        internal List<Video> GetMoviesBySite(long siteId)
        {
            return _dbManager.GetMoviesBySite(siteId);
        }

        internal List<Series> GetTVShowsBySite(long siteId)
        {
            return _dbManager.GetTVShowsBySite(siteId);
        }

        internal Video GetMovieByID(long videoId)
        {
            return _dbManager.GetMovieByID(videoId);
        }

        internal TVShow GetTVShowByID(long videoId)
        {
            return _dbManager.GetTVShowByID(videoId);
        }

        internal VideosLink GetVideoLinkByID(long linkId)
        {
            return _dbManager.GetVideoLinkByID(linkId);
        }

        internal TVShowsLink GetTVShowLinkByID(long linkId)
        {
            return _dbManager.GetTVShowLinkByID(linkId);
        }

        internal List<Series> GetSeriesList()
        {
            return _dbManager.GetSeriesList();
        }

        internal List<TVShow> GetTVShowsBySeries(long seriesId)
        {
            return _dbManager.GetTVShowsBySeries(seriesId);
        }

        internal List<long> GetHebSubSeasonsbySeriesId(long seriesId)
        {
            return _dbManager.GetHebSubSeasonsbySeriesId(seriesId);
        }

        internal List<long> GetSeasonsbySeriesAndSiteId(long siteId, long seriesId)
        {
            return _dbManager.GetSeasonsbySeriesAndSiteId(siteId, seriesId);
        }

        internal List<long> GetSeasonsbySeriesId(long seriesId)
        {
            return _dbManager.GetSeasonsbySeriesId(seriesId);
        }

        internal List<TVShow> GetHebSubTVShowsBySeasonId(long seriesId, long seasonNumber)
        {
            return _dbManager.GetHebSubTVShowsBySeasonId(seriesId, seasonNumber);
        }

        internal List<TVShow> GetTVShowsBySeasonAndSiteId(long siteId, long seriesId, long seasonNumber)
        {
            return _dbManager.GetTVShowsBySeasonAndSiteId(siteId, seriesId, seasonNumber);
        }

        internal List<TVShow> GetTVShowsBySeasonId(long seriesId, long seasonNumber)
        {
            return _dbManager.GetTVShowsBySeasonId(seriesId, seasonNumber);
        }

        //public void Maintain()
        //{
        //    var videos = _dbManager.GetAllVideos();
        //    foreach (var video in videos)
        //    {
        //        //video.Is_Heb_Dub = IsHebDub(video.VideoName);
        //        //video.Is_Heb_Sub = IsHebSub(video.VideoName);
        //    }
        //    _dbManager.SaveAllVideos();
        //}

        internal void InsertMegaVideoCookie(string cookieCode, DateTime expiredDate)
        {
            _dbManager.InsertMegaVideoCookie(cookieCode, expiredDate);
        }

        internal List<Video> GetMoviesBySearchText(string searchText)
        {
            return _dbManager.GetMoviesBySearchText(searchText);
        }

        internal List<Series> GetTVShowsBySearchText(string searchText)
        {
            return _dbManager.GetTVShowsBySearchText(searchText);
        }
    }
}